<template>
  <div class="app-container">
    <div class="app-title">
      <span>人员档案查询</span>
    </div>
    <div class="filter-container">
      <el-row :gutter="20">
        <div class="filter-container">
          <el-col
            style="text-align:right;margin-top:-3px;"
            :span="24"
          >
            <el-button
              size="small"
              class="filter-item"
              style="margin-left:10px;"
              type="primary"
              @click="query"
            >
              查询
            </el-button>
            <!-- <el-button
              size="small"
              class="filter-item"
              style="margin-left:10px;"
              type="primary"
            >
              打印
            </el-button>
            <el-button
              size="small"
              class="filter-item"
              style="margin-left:10px;"
              type="primary"
            >
              导出
            </el-button> -->

          </el-col>
        </div>
      </el-row>
      <el-dialog  v-dialogDrag  
        :close-on-click-modal="false"
        :title="dialogStatus"
        :visible.sync="dialogEditFormVisible"
        style="padding:0px"
        width="45%"
      >
        <el-form
          ref="dataForm"
          class="dialog-Backgroundcolor"
          :model="listQuery"
          label-position="right"
        >
          <div class="filter-container">
            <el-row>
              <el-col :span="12">
                <el-form-item label="人员姓名：">
                  <el-input v-model="listQuery.personName" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="身份证号码：">
                  <el-input v-model="listQuery.idenNo" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-button
                  size="small"
                  class="filter-item"
                  style="margin-top:10px; float:right"
                  type="primary"
                  @click="handleQuery"
                >
                  查询
                </el-button>
              </el-col>
            </el-row>

          </div>
        </el-form>
      </el-dialog>
      <el-row>

        <el-col :span="12">

          <el-select
            v-model="listQuery.state"
            class="filter-item"
            placeholder="--请选择--"
            @change="handleFilter"
          >
            <el-option
              label="全部人员"
              value=""
            />
            <el-option
              label="在职人员"
              value="3"
            />
            <el-option
              label="离退休人员"
              value="1"
            />
          </el-select>

        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <span style="font-size: 16px">单位名称：{{ agencyName }}</span>
        </el-col>

      </el-row>
    </div>
    <!-- 表格部分 -->
    <el-table
      ref="singalTable"
      v-loading="listLoading"
      :data="list"
      :stripe="true"
      fit
      :height="tableHeight"
      style="width: 100%;"
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
    >
      <template slot="empty">
        <tableEmpty />
      </template>
      <el-table-column
        type="selection"
        width="55"
        align="center"
        fixed
      />
      <el-table-column
        type="index"
        align="center"
        width="50"
        label="序号"
        fixed
      />
      <el-table-column
        label="姓名"
        align="center"
        fixed
      >
        <template slot-scope="{row}">
          <el-link
            type="primary"
            @click="viewInfo(row)"
          >
            <span>{{ row.personName }}</span>
          </el-link>
        </template>
      </el-table-column>
      <el-table-column
        label="身份证号码"
        align="center"
        width="200"
        fixed
      >
        <template slot-scope="{row}">
          <span>{{ row.idenNo  }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="性别"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.sexCode | sexFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="民族"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.natCode | natFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="人员状态"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.state | personFlagFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="学历"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.schRecCode | schrecFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="职务"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.posCode | posgrFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="职级"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.grCode | grFilter(that) }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column-->
      <!--        label="是否在编"-->
      <!--        align="center"-->
      <!--        width="100"-->
      <!--      >-->
      <!--        <template slot-scope="{row}">-->
      <!--          <span>{{ row.isAuth | authFilter(that) }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column
        label="工资级别"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.salaGrCode | salagrFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="出生日期"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.birthday }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="入职时间"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.enterAgencyDate }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="离退休时间"
        align="center"
        width="120"
      >
        <template slot-scope="{row}">
          <span>{{ row.retireDate }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="参加工作时间"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.workInitDate }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="调离时间"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.endDate }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column
        label="死亡时间"
        align="center"
        width="100"
      >
        <template slot-scope="{row}">
          <span>{{ row.deadDate }}</span>
        </template>
      </el-table-column> -->
      <el-table-column
        label="备注"
        align="center"
        width="200"
      >
        <template slot-scope="{row}">
          <span>{{ row.remarks }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
    <PersonSalaryInfo ref="personSalaryInfo" />
  </div>
</template>

<script>
import { getPersonInfoList } from '@/api/personInfoMaintenance/personChangeEdit'
import {
  getSex,
  getNat,
  getSchrec,
  getPosgr,
  getGr,
  getSalagr
} from '@/api/baseInfo'
import PersonSalaryInfo from '@/components/personSalaryInfo/personSalaryInfo'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import tableEmpty from '@/components/table/empty'
export default {
  name: 'ComplexTable',
  components: { Pagination, tableEmpty, PersonSalaryInfo },
  directives: { waves },
  filters: {
    // 性别过滤器
    sexFilter(type, that) {
      const sexLists = that.sexLists
      for (const item in sexLists) {
        if (sexLists[item].code === type) {
          return sexLists[item].name
        }
      }
    },
    // 民族过滤器
    natFilter(type, that) {
      const natLists = that.natLists
      for (const item in natLists) {
        if (natLists[item].code === type) {
          return natLists[item].name
        }
      }
    },
    // 学历过滤器
    schrecFilter(type, that) {
      const schrecLists = that.schrecLists
      for (const item in schrecLists) {
        if (schrecLists[item].code === type) {
          return schrecLists[item].name
        }
      }
    },
    // 职务过滤器
    posgrFilter(type, that) {
      const posgrLists = that.posgrLists
      for (const item in posgrLists) {
        if (posgrLists[item].code === type) {
          return posgrLists[item].name
        }
      }
    },
    // 职级过滤器
    grFilter(type, that) {
      const grLists = that.grLists
      for (const item in grLists) {
        if (grLists[item].code === type) {
          return grLists[item].name
        }
      }
    },
    // 工资级别过滤器
    salagrFilter(type, that) {
      const salagrLists = that.salagrLists
      for (const item in salagrLists) {
        if (salagrLists[item].code === type) {
          return salagrLists[item].name
        }
      }
    },
    // 人员状态过滤器
    personFlagFilter(type, that) {
      if (type === '1') {
        return '离退休'
      } else if (type === '3') {
        return '在职'
      }
    },
    // 是否在编过滤器
    authFilter(type, that) {
      if (type === 1) {
        return '是'
      } else if (type === 0) {
        return '否'
      }
    },
    //号码脱敏显示
    hideMiddle(val) {
      if(val!== undefined){
        return `${val.substring(0,3)}****${val.substring(val.length-3)}`
      }else{
        return val
      }
    }
  },
  data() {
    return {
      tableHeight: null,
      dialogEditFormVisible: false,
      dialogStatus: '',
      that: this,
      listLoading: false,
      list: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: 200,
        personName: '',
        idenNo: '',
        state: '',
        agencyId: this.$store.getters.userInfo.agencyId
      },
      agencyName: this.$store.getters.userInfo.agencyName,
      sexLists: [], // 性别
      natLists: [], // 民族
      schrecLists: [], // 学历
      posgrLists: [], // 职务
      grLists: [], // 职级
      salagrLists: [] // 工资级别
    }
  },
  watch: {
    list(val) {
      this.doLayout()
    }
  },
  mounted() {
    this.getTbHeight()
  },
  beforeMount() {
    window.addEventListener('resize', this.getTbHeight)
  },
  created() {
    this.initBaseInfo()
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getPersonInfoList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    // 查询
    handleFilter() {
      this.getList()
    },
    // 查看详情
    viewInfo(currenRow) {
      this.$refs.personSalaryInfo.viewInfo(currenRow.id, undefined, this.listQuery.agencyId)
    },
    // 初始化基础数据
    initBaseInfo() {
      getSex().then(response => {
        this.sexLists = Object.assign({}, response.data)
      })
      getNat().then(response => {
        this.natLists = Object.assign({}, response.data)
      })
      getSchrec().then(response => {
        this.schrecLists = Object.assign({}, response.data)
      })
      getPosgr().then(response => {
        this.posgrLists = Object.assign({}, response.data)
      })
      getGr().then(response => {
        this.grLists = Object.assign({}, response.data)
      })
      getSalagr().then(response => {
        this.salagrLists = Object.assign({}, response.data)
      })
    },
    query() {
      this.dialogStatus = '查询'
      this.dialogEditFormVisible = true
    },
    handleQuery() {
      this.dialogStatus = '人员档案查询'
      this.listQuery.page = 1
      // this.listQuery.limit = 10
      this.getList()
      this.dialogEditFormVisible = false
    },
    // 获取table高度的方法
    getTbHeight() {
      this.tableHeight = window.innerHeight - 269
    },
    /* 重新渲染table组件 */
    doLayout() {
      this.$nextTick(() => {
        this.$refs.singalTable.doLayout()
      })
    }
  }
}
</script>
<style scoped lang="scss">
.app-title {
  margin-bottom: 15px;
  height: 20px;
  border-bottom: 1px solid #dcdfe6;
}
</style>
